// //构造函数模式
// function Person(name,age,job){
//     this.name = name;
//     this.age = age;
//     this.job = job;
//     this.sayName = function(){
//         console.log(this.name);
//     }
//     // return {desc:'this is not empty obj'}
// }
// let person1 = new Person('silly',25,'a piano');
// let person2 = new Person('smith',26,'a computer');
// console.log(person1);//{ desc: 'this is not empty obj' }
// console.log(person2);
// person1.sayName();//silly
// person2.sayName();//smith
// console.log( person1 instanceof Person);//true
// console.log( person2 instanceof Person);//true
// console.log( person2.constructor === Person);//true
// console.log(person2.constructor === Object);//false

// // console.log( person1 instanceof person2);ReferenceError: person2 is not defined

// function Person(name,age){
//     this.name = name;
//     this.age = age;
//     this.sayName = function(){
//         console.log(this.name);
//     }
// }
//采用标准的构造函数方式调用
// let person = new Person('silly',25);
// person.sayName();//silly
// //采用普通函数方式调用
// Person('smith',20);
// let global = function() { 
//     return this; 
//    }();
// global.sayName();//smith
// console.log(global === globalThis);//true
// //在另外一个对象作用域中调用
// let obj = {};
// Person.call(obj, "Kristen", 25);
// console.log(obj);//{ name: 'Kristen', age: 25, sayName: [Function] }
// obj.sayName();//Kristen

// function Person(name,age){
//     this.name = name;
//     this.age = age;
//     this.sayName = function(){
//         console.log(this.name);
//     }
//     //等价于
//     // this.sayName = new Function('console.log(this.name);')
// }
// let person = new Person('alice',24);
// let person1 = new Person('alice',24);
// person.sayName();//alice
// console.log(person.sayName === person1.sayName);//false

function Person(name,age){
    this.name = name;
    this.age = age;
    this.sayName = sayName;
}
function sayName(){
    console.log(this.name);
}
let person1 = new Person('silly',25);
let person2 = new Person('scott',26);
person1.sayName();//silly
person2.sayName();//scott
console.log(person1.sayName === person2.sayName);//true
